package com.hutianao.gqgl.login.control;

import com.hutianao.gqgl.mybatis.entity.PtUserEntity;
import com.hutianao.gqgl.spring.control.BaseController;
import com.hutianao.gqgl.user.service.UserService;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpSession;

import javax.servlet.http.HttpServletRequest;

import java.util.Locale;

@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {
    private Logger logger = LogManager.getLogger(this.getClass());

    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public String login(HttpServletRequest request){
        Locale locale =LocaleContextHolder.getLocale();
        request.getSession().setAttribute("siteLanguage",locale.toString());
        logger.info("login");
        return "login/login";

    }
    @RequestMapping("/fail")
    public String fail(HttpServletRequest request){
        HttpSession session = request.getSession();
        do{
            String erorrNum = (String)session.getAttribute("error.num.start.auth");
            if(erorrNum == null){
                break;
            }
            Integer erorrNumInt = Integer.parseInt(erorrNum) ;

            Integer loginErro = (Integer) session.getAttribute("loginErro");
            if(loginErro == null){
                loginErro = 1;
            }
            if(loginErro >= erorrNumInt){
                session.setAttribute("isAuthCode","true");
            }
            loginErro++;
            session.setAttribute("loginErro",loginErro);

        }while (false);

        return "redirect:/login/login?error";
    }
    @RequestMapping("/main")
    public String main(){
        logger.info("main");
        return "redirect:/";

    }


    @RequestMapping("/toRegister")
    public String toRegister(HttpServletRequest request){
        Locale locale =LocaleContextHolder.getLocale();
        request.getSession().setAttribute("siteLanguage",locale.toString());
        logger.info("toRegister");
        return "login/register";
    }
    @RequestMapping("/toForget")
    public String toForget(HttpServletRequest request){
        Locale locale =LocaleContextHolder.getLocale();
        request.getSession().setAttribute("siteLanguage",locale.toString());
        logger.info("toForget");
        return "login/forget";
    }

    @RequestMapping("/toPasswordUpdate")
    public String toPasswordUpdate(HttpServletRequest request){
        Locale locale =LocaleContextHolder.getLocale();
        request.getSession().setAttribute("siteLanguage",locale.toString());
        logger.info("toPasswordUpdate");
        return "login/PasswordUpdate";
    }


    @RequestMapping("/addUserInfo")
    @ResponseBody
    public String addUserInfo(PtUserEntity ptUserEntity){
        logger.info(ptUserEntity);
        String result = userService.addUserInfo(ptUserEntity);
        if(result.contains("error")){
            return jsonFail(result);
        }else{
            return jsonSuccess(result);
        }
    }

    @RequestMapping("/checkPwd")
    @ResponseBody
    public String checkPwd(String username, String password, String newpassword){
        String result = userService.checkPwd(username,password,newpassword);
        if(result.contains("error")){
            return jsonFail(result);
        }else{
            return jsonSuccess(result);
        }
    }
}


